﻿using BLL;
using Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;

namespace GoShopping.tools
{
    /// <summary>
    /// User 的摘要说明
    /// </summary>
    public class User : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string t = HttpContext.Current.Request.Form["t"];//干什么用的
            switch (t)
            {
                case "3":
                    LogIn(context);
                    break;
                case "4":
                    Regiser(context); 
                    break;
                case "5":
                    update(context);
                    break;
            }
        }
        //修改
        private void update(HttpContext context)
        {
            //接受前端传来的数据
            Models.UserInfo user=new Models.UserInfo();
            user.UserName = context.Request.Form["userName"].ToString();
            user.UserPwd = context.Request.Form["Pwd"].ToString();
            user.Phone = context.Request.Form["Phone"].ToString();
            user.QQ = context.Request.Form["QQ"].ToString();
            user.UserID = int.Parse(context.Request.Form["UserID"]);
            //调用方法进行修改
            bool res=UserInfoBLL.updateByIDBLL(user);
            if (res)
            {
                context.Response.Write("{\"status\":\"修改成功!\"}");
            }
        }

        //注册
        private void Regiser(HttpContext context)
        {
            //获取从前台传递过来的用户名
            string userName = HttpContext.Current.Request.Form["username"].ToString();
            //获取从前台传递过来的密码
            string pwd = HttpContext.Current.Request.Form["pwd"].ToString();
            //true有数据，false为没数据
            bool res= UserInfoBLL.selectByNameBLL(userName);
            if (res)
            {
                context.Response.Write("{\"status\":\"用户名重复!\"}");
            }
            else
            {
                bool resp=UserInfoBLL.addUserBLL(userName, pwd);
                if (resp)
                {
                    context.Response.Write("{\"status\":\"注册成功，请重新登录！\"}");
                }
            }
        }

        //登录
        private void LogIn(HttpContext context)
        {
            //获取从前台传递过来的用户名
            string userName = HttpContext.Current.Request.Form["count"].ToString();
            //获取从前台传递过来的密码
            string pwd = HttpContext.Current.Request.Form["pwd"].ToString();
            //获取用户当前状态 0：正常，1：禁用
            bool res = UserInfoBLL.loginBLL(userName, pwd);
            if (res)
            {
                string state = UserInfoBLL.getStateBLL(userName);
                if (state == "0")
                {
                    //调用方法获取数据
                    List<Models.UserInfo> list = new List<Models.UserInfo>();
                    Models.UserInfo userInfo = UserInfoBLL.getAllByUsername(userName);
                    list.Add(userInfo);
                    //把数据转换为JSON格式
                    JObject obj = new JObject();
                    JArray array = JArray.FromObject(list);
                    obj.Add("res", array);
                    context.Response.Write(obj);
                }
                else
                {
                    context.Response.Write("{\"status\":\"你已经被拉黑了！\"}");
                }
            }
            else
            {
                context.Response.Write("{\"status\":\"用户名密码不正确\"}");
            }
        }
        
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}